﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using WmWebSystem.Models;
using WmWebSystem.Service;

namespace WmWebSystem.DbHelper
{
    public static class NewsHelper
    {
        //[ValidateInput(false)]
        public static int  CreateNewToNews(string title, string cont, string type, string typeName)
        {
            WmDb mydb = new WmDb();
            News news = new News();

            news.AddTime = DateTime.Now;
            news.ClickNum = 1;
            news.Content = cont;
            news.Editor = HttpContext.Current.User.Identity.Name;
            news.Title = title;
            news.Type = Convert.ToInt32(type);
            news.TypeName = typeName;
            try
            {
                mydb.Newss.Add(news);
                mydb.SaveChanges();
                //将记录写入日志
                string p1 = "Add new to News, The NewsTitle:" + title;
                WriteLog.WriteSysLog(p1);
                return 1;
            }
            catch (FieldAccessException ex)
            {
                //失败，进行回滚操作  记录异常
                WriteLog.WriteSysLog(ex.ToString());
                return 0;
            }


        }

        public static void DeleteNewsFrom(int id)
        {
            WmDb mydb = new WmDb();
            var news = mydb.Newss.Find(id);

            //删除项目
            try
            {
                mydb.Newss.Remove(news);
                mydb.SaveChanges();
                //将记录写入日志
                string p1 = "Delete a news from Newss, where NewsId:" + id;
                WriteLog.WriteSysLog(p1);
            }
            catch (FieldAccessException ex)
            {
                //失败，进行回滚操作  记录异常

                WriteLog.WriteSysLog(ex.ToString());
            }

        }

        public static IEnumerable<News> FindAllNewsby(int type) 
        {
            WmDb mydb=new WmDb();
            var news = from n in mydb.Newss
                       where n.Type == type
                       orderby n.AddTime descending
                       select n;
            return news;
        }

        public static News FindOneNewsby(int type)
        {
            WmDb mydb = new WmDb();
            var news = (from n in mydb.Newss
                       where n.Type == type
                       orderby n.AddTime descending
                       select n).FirstOrDefault();
            return news;
        }

        public static IEnumerable<News> FindTopSix(int type) 
        {
            WmDb mydb = new WmDb();
            var news = (from n in mydb.Newss
                        where n.Type == type
                        orderby n.AddTime descending
                        select n).Take(6);
            return news;
        }

        public static IEnumerable<News> SearchNewsby(string title, string cont, DateTime start, DateTime end)
        {
            WmDb mydb = new WmDb();

            //1111
            if (title != "" && cont != "" && start != null && end != null)
            {
                var news = from n in mydb.Newss
                           where n.Title.Contains(title) && n.Content.Contains(cont) && n.AddTime >= start && n.AddTime <= end
                           orderby n.AddTime descending
                           select n;
                return news;
            }
            //1110
            else if (title != "" && cont != "" && start != null && end == null)
            {
                var news = from n in mydb.Newss
                           where n.Title.Contains(title) && n.Content.Contains(cont) && n.AddTime >= start
                           orderby n.AddTime descending
                           select n;
                return news;
            }
            //1101
            else if (title != "" && cont != "" && start == null && end != null)
            {
                var news = from n in mydb.Newss
                           where n.Title.Contains(title) && n.Content.Contains(cont) && n.AddTime <= end
                           orderby n.AddTime descending
                           select n;
                return news;
            }
            //1011
            else if (title != "" && cont == "" && start != null && end != null)
            {
                var news = from n in mydb.Newss
                           where n.Title.Contains(title) && n.AddTime >= start && n.AddTime <= end
                           orderby n.AddTime descending
                           select n;
                return news;
            }
            //0111
            else if (title == "" && cont != "" && start != null && end != null)
            {
                var news = from n in mydb.Newss
                           where n.Content.Contains(cont) && n.AddTime >= start && n.AddTime <= end
                           orderby n.AddTime descending
                           select n;
                return news;
            }
            //0011
            else if (title == "" && cont == "" && start != null && end != null)
            {
                var news = from n in mydb.Newss
                           where n.AddTime >= start && n.AddTime <= end
                           orderby n.AddTime descending
                           select n;
                return news;
            }
            //1100
            else if (title != "" && cont != "" && start == null && end == null)
            {
                var news = from n in mydb.Newss
                           where n.Title.Contains(title) && n.Content.Contains(cont)
                           orderby n.AddTime descending
                           select n;
                return news;
            }
            //1010
            else if (title != "" && cont == "" && start != null && end == null)
            {
                var news = from n in mydb.Newss
                           where n.Title.Contains(title) && n.AddTime <= end
                           orderby n.AddTime descending
                           select n;
                return news;
            }
            //0101
            else if (title == "" && cont != "" && start == null && end != null)
            {
                var news = from n in mydb.Newss
                           where n.Content.Contains(cont) && n.AddTime <= end
                           orderby n.AddTime descending
                           select n;
                return news;
            }
            //0110
            else if (title == "" && cont != "" && start != null && end == null)
            {
                var news = from n in mydb.Newss
                           where n.Content.Contains(cont) && n.AddTime >= start
                           orderby n.AddTime descending
                           select n;
                return news;
            }
            //1001
            else if (title != "" && cont == "" && start == null && end != null)
            {
                var news = from n in mydb.Newss
                           where n.Title.Contains(title) && n.AddTime <= end
                           orderby n.AddTime descending
                           select n;
                return news;
            }
            //1000
            else if (title != "" && cont == "" && start == null && end == null)
            {
                var news = from n in mydb.Newss
                           where n.Title.Contains(title)
                           orderby n.AddTime descending
                           select n;
                return news;
            }
            //0100
            else if (title == "" && cont != "" && start == null && end == null)
            {
                var news = from n in mydb.Newss
                           where n.Content.Contains(cont)
                           orderby n.AddTime descending
                           select n;
                return news;
            }
            //0010
            else if (title == "" && cont == "" && start != null && end == null)
            {
                var news = from n in mydb.Newss
                           where n.AddTime >= start
                           orderby n.AddTime descending
                           select n;
                return news;
            }
            //0001
            else if (title == "" && cont == "" && start == null && end != null)
            {
                var news = from n in mydb.Newss
                           where n.AddTime <= end
                           orderby n.AddTime descending
                           select n;
                return news;
            }
            else 
            {
                return FindAllNewsby(1);
            }
        }

    }
}
